<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\DB;

use QL\QueryList;

class CollectController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.collect.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        // 设置执行时间限制 0为不限制时间
        set_time_limit(0);

        $ql = QueryList::get($_GET['url'])->encoding('utf-8','gb2312')->removeHead();

        // 采集表的每行内容
        $tableRows = $ql->find('.content tr:gt(0)')->map(function ($row) {
            return $row->find('td')->texts()->all();
        });

        // 采集表中链接
        $url = $ql->find('.content tr:gt(0)')->map(function ($row) {
            return $row->find('td>a')->attr('href');
        });

        for ($i = 0; $i < count($tableRows); $i++) {

            if (isset($tableRows[$i][2])) {

                if (preg_match("/【固顶通宵】精品推荐/i", $tableRows[$i][2])) {
                    $type = '0,0,0,1,0';
                    $open_time = date("Y-m-d 01:00:00", strtotime("-1 day"));

                } else if (preg_match("/精品极力推荐/i", $tableRows[$i][2])) {
                    $type = '0,0,0,0,1';
                    $open_time = date('Y/m/d 01:00:00');

                } else if (preg_match("/通宵推荐/i", $tableRows[$i][2])) {
                    $type = '0,0,1,0,0';
                    $a = str_replace('〖通宵推荐〗','',$tableRows[$i][2]);
                    $open_time = date('Y/' . str_replace(array('月','日'),array('',''),$a). ' 01:00:00');

                } else if (preg_match("/固顶通宵/i", $tableRows[$i][2])) {
                    $type = '0,0,0,1,0';
                    $a = str_replace('【固顶通宵】','',$tableRows[$i][2]);
                    $open_time = date('Y/' . str_replace(array('月','日'),array('',''),$a). ' 01:00:00');

                } else if (preg_match("/精品极力推荐/i", $tableRows[$i][2])) {
                    $type = '0,0,0,0,1';
                    $open_time = date('Y/m/d 01:00:00');

                } else {
                    $type = '1,0,0,0,0';
                    $a = str_replace(array('月','日','点','分开发'),array('','',':',''),$tableRows[$i][2]);
                    $a= str_replace('开放','',$a);
                    $a= str_replace('分','',$a);
                    $open_time = date('Y/'.$a);
                }



                //if ( preg_match("/合击/i",$tableRows[$i][0]) || preg_match("/合击/i",$tableRows[$i][1]) || preg_match("/合击/i",$tableRows[$i][3]) || preg_match("/合击/i",$tableRows[$i][4]) || preg_match("/合击/i",$tableRows[$i][5]) ) {

                DB::table('game_data')->updateOrInsert(
                    ['url' => $url[$i]],
                    ['type' => $type,
                        'server_name' => $tableRows[$i][0],
                        'server_ip' => $tableRows[$i][1],
                        'open_time' => $open_time,
                        'network' => $tableRows[$i][3],
                        'introduction' => substr($tableRows[$i][4], 0, -7),
                        'qq' => $tableRows[$i][5],
                        'url' => $url[$i],
                        'audit' => 1,
                        'count' => 1,
                        'consume' => 0,
                        'publisher' => 'admin',
                        'release_date' => date('Y-m-d H:i:s')
                    ]);
                //}

            }
        }

        return response()->json(['message' => '采集成功。', 'success' => 'true']);

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        // 设置执行时间限制 0为不限制时间
        set_time_limit(0);

        $ql = QueryList::get($request->url)->encoding('utf-8','gb2312')->removeHead();

        // 采集表的每行内容
        $tableRows = $ql->find('.content tr:gt(0)')->map(function ($row) {
            return $row->find('td')->texts()->all();
        });

        // 采集表中链接
        $url = $ql->find('.content tr:gt(0)')->map(function ($row) {
            return $row->find('td>a')->attr('href');
        });

        for ($i = 0; $i < count($tableRows); $i++) {

            if (isset($tableRows[$i][2])) {

                if (preg_match("/【固顶通宵】精品推荐/i", $tableRows[$i][2])) {
                    $type = '0,0,0,1,0';
                    $open_time = date("Y-m-d 01:00:00", strtotime("-1 day"));

                } else if (preg_match("/精品极力推荐/i", $tableRows[$i][2])) {
                    $type = '0,0,0,0,1';
                    $open_time = date('Y/m/d 01:00:00');

                } else if (preg_match("/通宵推荐/i", $tableRows[$i][2])) {
                    $type = '0,0,1,0,0';
                    $a = str_replace('〖通宵推荐〗','',$tableRows[$i][2]);
                    $open_time = date('Y/' . str_replace(array('月','日'),array('',''),$a). ' 01:00:00');

                } else if (preg_match("/固顶通宵/i", $tableRows[$i][2])) {
                    $type = '0,0,0,1,0';
                    $a = str_replace('【固顶通宵】','',$tableRows[$i][2]);
                    $open_time = date('Y/' . str_replace(array('月','日'),array('',''),$a). ' 01:00:00');

                } else if (preg_match("/精品极力推荐/i", $tableRows[$i][2])) {
                    $type = '0,0,0,0,1';
                    $open_time = date('Y/m/d 01:00:00');

                } else {
                    $type = '1,0,0,0,0';
                    $a = str_replace(array('月','日','点','分开发'),array('','',':',''),$tableRows[$i][2]);
                    $a= str_replace('开放','',$a);
                    $a= str_replace('分','',$a);
                    $open_time = date('Y/'.$a);
                }



                //if ( preg_match("/合击/i",$tableRows[$i][0]) || preg_match("/合击/i",$tableRows[$i][1]) || preg_match("/合击/i",$tableRows[$i][3]) || preg_match("/合击/i",$tableRows[$i][4]) || preg_match("/合击/i",$tableRows[$i][5]) ) {

                    DB::table('game_data')->updateOrInsert(
                        ['url' => $url[$i]],
                        ['type' => $type,
                            'server_name' => $tableRows[$i][0],
                            'server_ip' => $tableRows[$i][1],
                            'open_time' => $open_time,
                            'network' => $tableRows[$i][3],
                            'introduction' => substr($tableRows[$i][4], 0, -7),
                            'qq' => $tableRows[$i][5],
                            'url' => $url[$i],
                            'audit' => 1,
                            'count' => 1,
                            'consume' => 0,
                            'publisher' => Auth::user()->username,
                            'release_date' => date('Y-m-d H:i:s')
                        ]);
               //}

            }
        }

        return response()->json(['message' => '采集成功。', 'success' => 'true']);

    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

}
